package com.hospital.yang.medicine.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hospital.yang.medicine.entity.DrugStocks;
import com.hospital.yang.medicine.entity.Medicnes;
import com.hospital.yang.medicine.service.MedicneStocksServic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 管理员
 * 药品补给及新增药品  申请
 */
@Controller
public class MedicnesController {

    @Autowired
    private MedicneStocksServic Service;

    /**
     * 分页查询每行五条数据
     * 库存补充    新增药品申请
     *
     * @param name        申请人
     * @param yaoName     药名
     * @param applyStatus 药类型
     * @param pageNum     当前页
     * @return 返回申请信息分页对象
     */
    @RequestMapping("/FySelect")
    public String FySelect(Model model, String name, String yaoName, @RequestParam(value = "applyStatus", defaultValue = "0") Integer applyStatus, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<Medicnes> medicnes = Service.selectStock(name, yaoName, applyStatus, pageNum);
        PageInfo<Medicnes> PageInfo = new PageInfo<>(medicnes);
        model.addAttribute("page", PageInfo);
        return "frontdesk/inMedicine/medicine";
    }

    /**
     * 申请表数据删除
     * @param id  编号删除 申请表数据
     * @return 成功返回 true
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Model model, Integer id) {
        boolean delete = Service.delete(id);
        return JSON.toJSONString(delete);
    }

    /**
     * 修改状态申请状态
     *
     * @param id 编号
     * @return 成功返回 true
     */
    @RequestMapping("/consent")
    @ResponseBody
    public String UPDATE(Model model, Integer id) {
        boolean b = Service.updateStatus(id);
        return JSON.toJSONString(b);
    }

    /**
     * 根据时间查询
     * 申请的药品信息
     *
     * @param time 时间
     * @return  根据时间返回申请信息分页对象
     */
    @RequestMapping("/details")
    @ResponseBody
    public String TjSelect(Model model, String time) {
        System.out.println(time + "申请的药品信息");
        List<DrugStocks> drugStocks = Service.selectDetails(time);
        return JSON.toJSONString(drugStocks);
    }

}
